package com.jhd.fuzhou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.EditTextView;
import com.jhd.fuzhou.vo.SupervisedUnitVo;
import com.jhd.fuzhou.vo.Supervisor;

import java.util.List;

import static com.jhd.fuzhou.view.login.MainActivity.supervisionInspectionVo;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {
    private List<SupervisedUnitVo> supervisedUnitVoList;
    private EditText textView;
    private TextView tv_Supervisor_1;
    private AlertDialog _alertDialog;
    private int checkPosition=-1;
    private Context context;
    public AddressAdapter(Context context, TextView textView, TextView tv_Supervisor_1, AlertDialog _alertDialog, List<SupervisedUnitVo> supervisedUnitVoList) {
        this.supervisedUnitVoList = supervisedUnitVoList;
        this._alertDialog = _alertDialog;
        this.textView = (EditText) textView;
        this.context = context;
        this.tv_Supervisor_1 = tv_Supervisor_1;
    }
    @NonNull
    @Override
    public AddressAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_choose, parent, false);
        AddressAdapter.ViewHolder viewHolder = new AddressAdapter.ViewHolder(itemView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull AddressAdapter.ViewHolder holder, int position) {
        holder.tv_choose.setText(supervisedUnitVoList.get(position).getManagementAddress());
        if(supervisedUnitVoList.get(position).getManagementAddress().equals(textView.getText().toString())){
            holder.image.setVisibility(View.VISIBLE);
        }else{
            holder.image.setVisibility(View.GONE);
        }
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(supervisedUnitVoList.get(position).getManagementAddress());
                EditTextView.TextViewChange(textView,context);
                holder.image.setVisibility(View.VISIBLE);
                _alertDialog.dismiss();
                checkPosition=position;
                notifyDataSetChanged();
            }
        });
        if(position==checkPosition){
            holder.image.setVisibility(View.VISIBLE);
        }else{
            if(checkPosition!=-1){
                holder.image.setVisibility(View.GONE);
            }
        }
    }
    @Override
    public int getItemCount() {
        return supervisedUnitVoList.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_choose;
        ImageView image;
        RelativeLayout relativeLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            this.tv_choose = itemView.findViewById(R.id.tv_choose);
            this.image = itemView.findViewById(R.id.image);
            this.relativeLayout = itemView.findViewById(R.id.relativeLayout);
        }
    }
}
